package com.bulletelc.apollopre.module.main.classify;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.entity.Classify;
import com.bulletelc.apollopre.manager.imageload.ImageLoaderManager;
import com.bulletelc.apollopre.widget.RoundedImageView;

import java.util.List;

/**
 * author: lhl
 * date  : 2018/01/13
 * desc  : TODO
 */

public class ClassifySecondAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<Classify> classifyList;
    private LayoutInflater inflater;

    public ClassifySecondAdapter(Context context, List<Classify> classifyList) {
        this.context = context;
        this.classifyList = classifyList;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getGroupCount() {
        return classifyList.size();
    }

    @Override
    public int getChildrenCount(int i) {
        if (classifyList.get(i).getClassifyList() != null) {
            return classifyList.get(i).getClassifyList().size();
        }
        return 0;
    }

    @Override
    public Object getGroup(int i) {
        return classifyList.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return classifyList.get(i).getClassifyList().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        GroupViewHolder viewHolder;
        Classify classify = classifyList.get(i);
        if (view == null) {
            viewHolder = new GroupViewHolder();
            view = inflater.inflate(R.layout.classify_second_item, viewGroup, false);
            viewHolder.llItemClassifySecond = view.findViewById(R.id.llItemClassifySecond);
            viewHolder.tvClassifyName = view.findViewById(R.id.tvClassifyName);
            viewHolder.ivClassifyImg = view.findViewById(R.id.ivClassifyImg);
            view.setTag(viewHolder);
        } else {
            viewHolder = (GroupViewHolder) view.getTag();
        }
        ImageLoaderManager.getInstance().loadImage(ImageLoaderManager.getDefaultOptions(viewHolder.ivClassifyImg, classify.getImageUrl()));
        if (!"".equals(classify.getImageUrl())) {
            viewHolder.tvClassifyName.setVisibility(View.GONE);
        } else {
            viewHolder.ivClassifyImg.setVisibility(View.GONE);
            viewHolder.tvClassifyName.setText(View.VISIBLE);
            viewHolder.tvClassifyName.setText(classify.getName());
        }
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        ChildViewHolder viewHolder;
        if (view == null) {
            viewHolder = new ChildViewHolder();
            view = inflater.inflate(R.layout.classify_second_item_child_item, viewGroup, false);
            viewHolder.tvName = view.findViewById(R.id.tvName);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ChildViewHolder) view.getTag();
        }
        viewHolder.tvName.setText(classifyList.get(i).getClassifyList().get(i1).getName());
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    class GroupViewHolder{
        TextView tvClassifyName;
        RoundedImageView ivClassifyImg;
        LinearLayout llItemClassifySecond;
    }

    class ChildViewHolder {
        TextView tvName;
    }

}
